HTTP Methods HEAD OPTIONS
HTTP Methods β HEAD & OPTIONS (Awareness + Debugging)β
HEAD and OPTIONS are not used daily by automation testers, but they are extremely important for debugging, security checks, and interviews.
6οΈβ£ HEAD Methodβ
What is HEAD?β
HEAD is similar to GET, but it returns only headers, not the response body.
Example:
HEAD /users/101
Meaning:
βCheck if the resource exists and return metadata, without fetching the body.β
Key Characteristics of HEADβ
| Aspect | Behavior |
|---|---|
| Purpose | Metadata check |
| Request Body | β Not allowed |
| Response Body | β Not returned |
| Safe | β Yes |
| Idempotent | β Yes |
| Cacheable | β Yes |
Why It Matters for Testersβ
- Faster: No payload means quicker responses.
- Lightweight: Useful for checking availability or permissions without downloading data.
When Testers Use HEADβ
- Check resource existence: Verify if a file or endpoint is available.
- Validate authentication/authorization: Ensure headers like
Authorizationwork correctly. - Measure response time: Benchmark API performance without payload overhead.
- Debug caching issues: Validate cache headers (
Cache-Control,ETag).
Real-World Exampleβ
Verifying file availability before download:
HEAD /files/report.pdf
Response Headers:
HTTP/1.1 200 OK
Content-Length: 10240
Content-Type: application/pdf
Code Snippet: Validating HEAD Responseβ
// RestAssured example
Response response = given()
.when()
.head("/files/report.pdf");
// Assertions
response.then().statusCode(200);
response.then().header("Content-Type", "application/pdf");
HEAD vs GET (Important Difference)β
| Aspect | HEAD | GET |
|---|---|---|
| Response body | β No | β Yes |
| Performance | Faster | Slower |
| Use case | Check availability | Fetch data |
Tester Ruleβ
Use
HEADwhen you donβt need the data, only the status or metadata.
Common HEAD Mistakes ββ
- Expecting response body:
HEADnever returns a body. - Using
HEADfor data validation: UseGETinstead. - Ignoring
HEADin API debugging: Misses critical metadata like cache headers.
7οΈβ£ OPTIONS Methodβ
What is OPTIONS?β
OPTIONS returns the allowed HTTP methods for a resource.
Example:
OPTIONS /users
Meaning:
βWhat operations are allowed on this endpoint?β
Key Characteristics of OPTIONSβ
| Aspect | Behavior |
|---|---|
| Purpose | Discover allowed methods |
| Request Body | β Not allowed |
| Safe | β Yes |
| Idempotent | β Yes |
Why It Matters for Testersβ
- Security Testing: Identify exposed methods that shouldnβt be public.
- CORS Debugging: Understand cross-origin restrictions.
- Environment Comparison: Compare allowed methods across environments.
OPTIONS & CORS (Very Important)β
OPTIONS is heavily used in CORS preflight requests.
Browser flow:
Browser
β
OPTIONS (preflight)
β
API Server
β
Allowed methods & headers
If OPTIONS fails:
- UI/API calls may fail with
403 Forbiddenor405 Method Not Allowed. - Automation tests may see unexpected errors.
Real-World Exampleβ
Validating allowed methods:
OPTIONS /users
Response Headers:
HTTP/1.1 200 OK
Allow: GET, POST, PUT, DELETE
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Origin: *
Code Snippet: Validating OPTIONS Responseβ
// RestAssured example
Response response = given()
.when()
.options("/users");
// Assertions
response.then().statusCode(200);
response.then().header("Allow", containsString("GET"));
response.then().header("Access-Control-Allow-Methods", containsString("POST"));
Automation Use Cases for OPTIONSβ
- Debug CORS issues: Identify why cross-origin requests fail.
- Validate allowed methods: Ensure endpoints support expected operations.
- Security testing: Detect over-exposed methods (e.g.,
DELETEpublicly accessible). - Environment comparison: Compare allowed methods between dev, staging, and prod.
Common OPTIONS Mistakes ββ
- Ignoring
OPTIONSfailures: Leads to missed CORS or method exposure issues. - Not understanding CORS preflight: Misinterprets browser errors.
- Assuming all methods are allowed: Fails to validate restricted methods.
Interview Questionsβ
Q: Why is OPTIONS used?
A: To identify allowed HTTP methods and support CORS preflight checks.
Q: Difference between GET and HEAD?
A: HEAD returns headers only, GET returns headers + body.
Key Takeaways π―β
HEADchecks metadata without body: Use for lightweight checks like availability or caching.OPTIONSlists allowed methods: Critical for CORS debugging and security testing.- Both are safe and idempotent.
- These methods are critical for debugging and interviews.
- Often involved in CORS issues.